package com.kaibing.hotswap.attach;

import com.sun.tools.attach.*;

import java.io.IOException;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws AgentLoadException, IOException, AgentInitializationException, AttachNotSupportedException {
        System.out.println("attach ----------------");
        System.out.println(Arrays.toString(args));
        System.out.println("attach start");
        String attachId = args[0];
        String agentPath = args[1];
        String oldClass = args[2];
        String newClassPath = args[3];
        VirtualMachine virtualMachine = VirtualMachine.attach(attachId);
        // 传入agent的jar包路径，args[2]=agent第一个参数
        virtualMachine.loadAgent(agentPath, oldClass + "," + newClassPath);
        virtualMachine.detach();

        System.out.println("attach over");
        System.out.println("attach ----------------");

    }
}
